package cn.fan.web;

import cn.fan.constants.DataBaseConstants;
import cn.fan.pojo.TaskRemind;
import cn.fan.service.RaskremindService;
import cn.fan.utils.MyPage;
import cn.fan.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("taskreminds")
public class RemindController {
    @Autowired
    private RaskremindService raskremindService;

    @GetMapping("test")
    public String test(){
        return "hello,test taskreminds!!";
    }

    @PostMapping("add")
    public R add(@RequestBody TaskRemind taskRemind){
        int i = raskremindService.add(taskRemind);
        return i > DataBaseConstants.NO_DATA ? R.ok("添加成功!") : R.error("添加失败!");
    }

    @PostMapping("list")
    public R list(@RequestBody TaskRemind taskRemind){
        List<TaskRemind> list = raskremindService.list(taskRemind,false);
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }

    @PostMapping("list/{pageSize}/{currentPage}")
    public R page(@RequestBody TaskRemind taskRemind,@PathVariable("pageSize") long pageSize,@PathVariable("currentPage") long currentPage){
        IPage<TaskRemind> page = raskremindService.page(taskRemind,currentPage,pageSize);
        MyPage pagination=new MyPage(page);
        Map<String,Object> res = new HashMap<>();
        res.put("pagination",pagination);
        return R.ok(res);
    }

    @GetMapping("getById/{id}")
    public TaskRemind getById(@PathVariable("id") int id){
        return raskremindService.getById(id);
    }

    @PostMapping("edit")
    public R edit(@RequestBody TaskRemind taskRemind){
        int i = raskremindService.edit(taskRemind);
        return i > DataBaseConstants.NO_DATA ? R.ok("修改成功!") : R.error("修改失败!");
    }

}
